#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#define tupleNum 	128*16*1024	

int main(int argc, char ** argv){
	int *sortCol, sortKey;
	int i,j;
	int key,value;

	if(argc != 4){
		printf("./aggBinaryGen colSize colNum keySize\n");
		exit(-1);
	}

	int colSize = atoi(argv[1]);
	int colNum = atoi(argv[2]);
	int keySize = atoi(argv[3]);

	sortCol = (int *)malloc(sizeof(int) * colNum);

	if(!sortCol){
		printf("failed to allocate memory!");
		exit(-1);
	}

	char buf[128]= {0};
	for(i=0;i<colNum;i++){
		memset(buf,0,sizeof(buf));
		sprintf(buf, "sortTable%d",i);
		sortCol[i] = open(buf,O_WRONLY|O_CREAT);
	}

	memset(buf,0,sizeof(buf));
	sprintf(buf,"sortKey");
	sortKey = open(buf, O_WRONLY|O_CREAT);

// here we don't care the actual value in the projected col

	for(i=0;i<tupleNum * colSize;i++){
		value = i%100;
		for(j=0;j<colNum;j++){
			write(sortCol[j],&value,sizeof(int));
		}
	}

// write out the key
	char * base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";	

	for(i=0; i<tupleNum * colSize; i++){
		memset(buf, 0, sizeof(buf));
		for(j=0;j<keySize;j++){
			buf[j] = base[random() % strlen(base)];
		}
		write(sortKey, buf, keySize);
	}

	for(i=0;i<colNum;i++){
		close(sortCol[i]);
	}

	close(sortKey);

	return 0;
}
